#include "stlObject.h"
#include "obj_parser/objLoader.h"

#include <glm/glm.hpp>
#include <string>
#include <iostream>


void WriteVert(std::ofstream &filout, float u, float v, float x, float y, float z)
{
	//, glm::vec3 &color

	//int re = int(255.f * color.x);
	//int gr = int(255.f * color.y);
	//int bl = int(255.f * color.z);
	//dexgel::LitVertex vert0 = { v0.x,v0.y,v0.z,n.x,n.y,n.z,D3DCOLOR_ARGB(255, re,gr,bl) };
	//dexgel::LitVertex vert1 = { v1.x,v1.y,v1.z,n.x,n.y,n.z,D3DCOLOR_ARGB(255, re,gr,bl) };
	//dexgel::LitVertex vert2 = { v2.x,v2.y,v2.z,n.x,n.y,n.z,D3DCOLOR_ARGB(255, re,gr,bl) };
	//vertBuf.push_back(  vert0  );
	//vertBuf.push_back(  vert1  );
	//vertBuf.push_back(  vert2  );


	float num = 1e5;
	
	filout << (int)(u*num) << " " <<(int)(v*num) << " " <<(int)(x*num) << " " << (int)(y*num) << " " << (int)(z*num) << std::endl;
}

int main(int argc, char* argv[])
{
	std::string file = "level.obj";
	if( argc > 1 )
	{
		file = std::string( argv[1] );

	}

	std::string file_type = file.substr(file.length()-1-3, file.length()-1 );
	if ( file_type != ".stl" && file_type != ".obj" )
	{
		return 1;
	}

	std::string file_out_name = std::string(file.substr(0, file.length()-1-3 ) ) + std::string(".txt");
	std::ofstream filout( file_out_name.c_str() );

	if ( file_type == ".stl" )
	{
		TorLib::StlObject myObj(file);
		assert( myObj.isLoaded == true );
		std::vector< glm::vec3 > vertices = myObj.getVertices();
		std::vector< glm::vec3 > normals = myObj.getNormals();

		for (size_t i=0; i<vertices.size(); i++ )
		{
			float num = 1e5;
			filout << (int)(vertices[i].x*num) << " " << (int)(vertices[i].y*num) << " " << (int)(vertices[i].z*num) << std::endl;
		
		}

	}
	else
	{
		objLoader *objData = new objLoader();
		if ( objData->load( (char*)file.c_str() ) == 0 )
		{
			return 1;
		}

		printf("Number of vertices: %i\n", objData->vertexCount);
		printf("Number of vertex normals: %i\n", objData->normalCount);
		printf("Number of texture coordinates: %i\n", objData->textureCount);
		printf("\n");

		printf("Number of faces: %i\n", objData->faceCount);
		for(int i=0; i<objData->faceCount; i++)
		{
			obj_face *o = objData->faceList[i];

			obj_vector *v0 = (objData->vertexList[ o->vertex_index[0] ]);
			obj_vector *v1 = (objData->vertexList[ o->vertex_index[1] ]);
			obj_vector *v2 = (objData->vertexList[ o->vertex_index[2] ]);

			glm::vec3 a = glm::vec3( (float)v0->e[0], (float)v0->e[1], (float)v0->e[2]  );
			glm::vec3 b = glm::vec3( (float)v1->e[0], (float)v1->e[1], (float)v1->e[2]  );
			glm::vec3 c = glm::vec3( (float)v2->e[0], (float)v2->e[1], (float)v2->e[2]  );
			glm::vec3 ab = b-a;
			glm::vec3 ac = c-a;
			glm::vec3 normal = glm::normalize( glm::cross( ab, ac ) );

			if ( objData->materialCount )
			{
				obj_material *mat = objData->materialList[ o->material_index ];
				glm::vec3 color = glm::vec3( mat->diff[0], mat->diff[1], mat->diff[2] );
			}


			obj_vector *texcoord0 = objData->textureList[ o->texture_index[0] ]; 
			obj_vector *texcoord1 = objData->textureList[ o->texture_index[1] ]; 
			obj_vector *texcoord2 = objData->textureList[ o->texture_index[2] ]; 

			float us[3];
			float vs[3];
			us[0] = texcoord0->e[0];
			us[1] = texcoord1->e[0];
			us[2] = texcoord2->e[0];

			vs[0] = texcoord0->e[1];
			vs[1] = texcoord1->e[1];
			vs[2] = texcoord2->e[1];

			//if(objData->materialCount)
			//{
			//	//WriteTriangle(filout, a,b,c,normal, us, vs, color);
			//}else
			{
				WriteVert(filout, texcoord0->e[0], texcoord0->e[1], (float)v0->e[0], (float)v0->e[1], (float)v0->e[2] );
				WriteVert(filout, texcoord1->e[0], texcoord1->e[1], (float)v1->e[0], (float)v1->e[1], (float)v1->e[2] );
				WriteVert(filout, texcoord2->e[0], texcoord2->e[1], (float)v2->e[0], (float)v2->e[1], (float)v2->e[2] );
			}

			if ( objData->faceList[i]->vertex_count == 4 )
			{
				obj_vector *v0 = (objData->vertexList[ o->vertex_index[0] ]);
				obj_vector *v2 = (objData->vertexList[ o->vertex_index[2] ]);
				obj_vector *v3 = (objData->vertexList[ o->vertex_index[3] ]);

				glm::vec3 a = glm::vec3( (float)v0->e[0], (float)v0->e[1], (float)v0->e[2]  );
				glm::vec3 b = glm::vec3( (float)v2->e[0], (float)v2->e[1], (float)v2->e[2]  );
				glm::vec3 c = glm::vec3( (float)v3->e[0], (float)v3->e[1], (float)v3->e[2]  );
				glm::vec3 ab = b-a;
				glm::vec3 ac = c-a;
				glm::vec3 normal = glm::normalize( glm::cross( ab, ac ) );

				obj_vector *texcoord0 = objData->textureList[ o->texture_index[0] ]; 
				obj_vector *texcoord1 = objData->textureList[ o->texture_index[1] ]; 
				obj_vector *texcoord2 = objData->textureList[ o->texture_index[2] ]; 

				float us[3];
				float vs[3];
				us[0] = texcoord0->e[0];
				us[1] = texcoord1->e[0];
				us[2] = texcoord2->e[0];

				vs[0] = texcoord0->e[1];
				vs[1] = texcoord1->e[1];
				vs[2] = texcoord2->e[1];

				WriteVert(filout, texcoord0->e[0], texcoord0->e[1], (float)v0->e[0], (float)v0->e[1], (float)v0->e[2] );
				WriteVert(filout, texcoord1->e[0], texcoord1->e[1], (float)v1->e[0], (float)v1->e[1], (float)v1->e[2] );
				WriteVert(filout, texcoord2->e[0], texcoord2->e[1], (float)v2->e[0], (float)v2->e[1], (float)v2->e[2] );
			}

		}

		delete(objData);

	}

	return 0;
}